<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="870"
    @close="close"
  >
    <el-form
      ref="formRef"
      inline
      label-width="140px"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="上级：" prop="ID">
        <el-tree-select
          v-model="form.ID"
          :check-strictly="true"
          :data="tree"
          node-key="ID"
          :props="defaultProps"
          :render-after-expand="false"
          show-checkbox
          @check="treeSelect"
        />
      </el-form-item>
      <el-form-item label="项目名称：" prop="fullName">
        <el-input v-model="form.fullName" />
      </el-form-item>
      <el-form-item label="项目值1：" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="项目值2：" prop="code2">
        <el-input v-model="form.code2" />
      </el-form-item>
      <el-form-item label="显示顺序：" prop="sortCode">
        <el-input v-model="form.sortCode" />
      </el-form-item>
      <el-form-item label="是否有效：" prop="Enabled">
        <el-switch v-model="form.Enabled" active-value="1" inactive-value="0" />
      </el-form-item>
      <el-form-item label="说明：" prop="remark">
        <el-input
          v-model="form.remark"
          autosize
          placeholder="请输入说明"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  // import VabIconSelector from '@/plugins/VabIconSelector'
  import { saveDetail, getDataDictionaryDetail } from '@/api/dictionary'

  export default defineComponent({
    name: 'DictionaryEdit',
    // components: { VabIconSelector },
    props: {
      tree: {
        type: Array,
        default: () => [],
      },
    },
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        params: {},
        form: {
          meta: {},
          parentId: '0',
        },
        data: [],
        queryForm: {},
        defaultProps: {
          children: 'ChildNodes',
          label: 'text',
        },
        rules: {
          code: [{ required: true, trigger: 'blur', message: '请输入项目值1' }],
          fullName: [
            { required: true, trigger: 'blur', message: '请输入项目名称' },
          ],
          // parentId: [
          //   { required: true, trigger: 'blur', message: '请输入上级按钮' },
          // ],
          ID: [
            { required: true, trigger: 'change', message: '请选择上级分类' },
          ],
          sortCode: [
            { required: false, trigger: 'blur', message: '请输入显示顺序' },
          ],
          remark: [{ required: false, trigger: 'blur', message: '请输入说明' }],
        },
        title: '',
        dialogFormVisible: false,
      })
      //获取详情数据
      const getdataDictionaryDetail = async () => {
        state.listLoading = true
        const {
          data: { data },
        } = await getDataDictionaryDetail(state.queryForm)
        state.form = data
        state.form.ID =
          data.parentId === '0' ? data.dataDictionaryId : data.parentId
        state.listLoading = false
      }
      //编辑传值
      const dialogType = ref('')
      const showEdit = (row, type) => {
        dialogType.value = type
        state.queryForm.dataDictionaryId =
          row.dataDictionaryId || row.DataDictionaryId
        if (type != 'edit') {
          state.title = '添加'
          state.form.dataDictionaryId = row.id
          state.form.ID = row.id
          // state.form.companyId = row.companyId
        } else {
          state.title = '编辑'
          state.queryForm.dataDictionaryDetailId = row.DataDictionaryDetailId
          getdataDictionaryDetail()
        }
        state.dialogFormVisible = true
      }
      //弹框关闭
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          meta: {},
          parentId: '0',
        }
        state.dialogFormVisible = false
      }
      //数据保存
      const save = async () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const form = JSON.parse(JSON.stringify(state.form))
            form.dataDictionaryId = state.queryForm.dataDictionaryId
            const { message } = await saveDetail(form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            emit('fetch-data', state.params)
            close()
          }
        })
      }
      const treeSelect = (node) => {
        if (node.DataDictionaryDetailId)
          state.form.parentId = node.DataDictionaryDetailId
        else state.form.parentId = 0
      }

      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
        getdataDictionaryDetail,
        treeSelect,
      }
    },
  })
</script>
<style scoped>
  .el-form {
    display: flex;
    flex-flow: wrap;
    justify-content: left;
  }
  .el-form >>> .el-input {
    width: 234px !important;
  }
  .el-form-item {
    width: 380px;
  }
</style>
